{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e218a6d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Iris 数据集：\n",
      "    sepal_length  sepal_width  petal_length  petal_width species\n",
      "0           5.1          3.5           1.4          0.2  setosa\n",
      "1           4.9          3.0           1.4          0.2  setosa\n",
      "2           4.7          3.2           1.3          0.2  setosa\n",
      "3           4.6          3.1           1.5          0.2  setosa\n",
      "4           5.0          3.6           1.4          0.2  setosa\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "# 从 seaborn 载入 Iris 数据集\n",
    "iris = sns.load_dataset('iris')\n",
    "print(\"Iris 数据集：\\n\", iris.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ec541008",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "从CSV读取的数据：\n",
      "    ID  Sepal.Length  Sepal.Width  Petal.Length  Petal.Width Species\n",
      "0   1           5.1          3.5           1.4          0.2  setosa\n",
      "1   2           4.9          3.0           1.4          0.2  setosa\n",
      "2   3           4.7          3.2           1.3          0.2  setosa\n",
      "3   4           4.6          3.1           1.5          0.2  setosa\n",
      "4   5           5.0          3.6           1.4          0.2  setosa\n"
     ]
    }
   ],
   "source": [
    "# 从 CSV 文件读取数据（这里可以使用已经下载的 Iris 数据集，可以看出来是一样滴）\n",
    "iris2 = pd.read_csv(\"./iris.csv\")\n",
    "print(\"从CSV读取的数据：\\n\", iris2.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6eb038d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "前5行数据：\n",
      "    sepal_length  sepal_width  petal_length  petal_width species\n",
      "0           5.1          3.5           1.4          0.2  setosa\n",
      "1           4.9          3.0           1.4          0.2  setosa\n",
      "2           4.7          3.2           1.3          0.2  setosa\n",
      "3           4.6          3.1           1.5          0.2  setosa\n",
      "4           5.0          3.6           1.4          0.2  setosa\n",
      "后5行数据：\n",
      "      sepal_length  sepal_width  petal_length  petal_width    species\n",
      "145           6.7          3.0           5.2          2.3  virginica\n",
      "146           6.3          2.5           5.0          1.9  virginica\n",
      "147           6.5          3.0           5.2          2.0  virginica\n",
      "148           6.2          3.4           5.4          2.3  virginica\n",
      "149           5.9          3.0           5.1          1.8  virginica\n"
     ]
    }
   ],
   "source": [
    "# 查看数据前5行\n",
    "print(\"前5行数据：\\n\", iris.head())\n",
    "\n",
    "# 查看数据后5行\n",
    "print(\"后5行数据：\\n\", iris.tail())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a913b6d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据基本信息：\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 150 entries, 0 to 149\n",
      "Data columns (total 5 columns):\n",
      " #   Column        Non-Null Count  Dtype  \n",
      "---  ------        --------------  -----  \n",
      " 0   sepal_length  150 non-null    float64\n",
      " 1   sepal_width   150 non-null    float64\n",
      " 2   petal_length  150 non-null    float64\n",
      " 3   petal_width   150 non-null    float64\n",
      " 4   species       150 non-null    object \n",
      "dtypes: float64(4), object(1)\n",
      "memory usage: 6.0+ KB\n",
      "数据统计汇总：\n",
      "        sepal_length  sepal_width  petal_length  petal_width\n",
      "count    150.000000   150.000000    150.000000   150.000000\n",
      "mean       5.843333     3.057333      3.758000     1.199333\n",
      "std        0.828066     0.435866      1.765298     0.762238\n",
      "min        4.300000     2.000000      1.000000     0.100000\n",
      "25%        5.100000     2.800000      1.600000     0.300000\n",
      "50%        5.800000     3.000000      4.350000     1.300000\n",
      "75%        6.400000     3.300000      5.100000     1.800000\n",
      "max        7.900000     4.400000      6.900000     2.500000\n"
     ]
    }
   ],
   "source": [
    "# 查看数据基本信息\n",
    "print(\"数据基本信息：\")\n",
    "iris.info()\n",
    "\n",
    "# 数据的统计汇总\n",
    "print(\"数据统计汇总：\\n\", iris.describe())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0bb50a9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "按种类分组的平均值：\n",
      "             sepal_length  sepal_width  petal_length  petal_width\n",
      "species                                                         \n",
      "setosa             5.006        3.428         1.462        0.246\n",
      "versicolor         5.936        2.770         4.260        1.326\n",
      "virginica          6.588        2.974         5.552        2.026\n"
     ]
    }
   ],
   "source": [
    "# 按种类（species）分组，并计算每组特征的均值\n",
    "grouped_iris = iris.groupby('species').mean()\n",
    "print(\"按种类分组的平均值：\\n\", grouped_iris)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "686d48a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "合并后的数据：\n",
      "    sepal_length  sepal_width  petal_length  petal_width species         region\n",
      "0           5.1          3.5           1.4          0.2  setosa  North America\n",
      "1           4.9          3.0           1.4          0.2  setosa  North America\n",
      "2           4.7          3.2           1.3          0.2  setosa  North America\n",
      "3           4.6          3.1           1.5          0.2  setosa  North America\n",
      "4           5.0          3.6           1.4          0.2  setosa  North America\n"
     ]
    }
   ],
   "source": [
    "# 创建另一个数据框用于合并\n",
    "data = {'species': ['setosa', 'versicolor', 'virginica'],\n",
    "        'region': ['North America', 'Europe', 'Asia']}\n",
    "df_region = pd.DataFrame(data)\n",
    "\n",
    "# 合并两个数据框（基于 species 列）\n",
    "merged_df = pd.merge(iris, df_region, on='species')\n",
    "print(\"合并后的数据：\\n\", merged_df.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4d07e87e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "添加缺失值后的数据：\n",
      "    sepal_length  sepal_width  petal_length  petal_width species\n",
      "0           NaN          3.5           1.4          0.2  setosa\n",
      "1           NaN          3.0           1.4          0.2  setosa\n",
      "2           NaN          3.2           1.3          0.2  setosa\n",
      "3           NaN          3.1           1.5          0.2  setosa\n",
      "4           NaN          3.6           1.4          0.2  setosa\n",
      "删除缺失值后的数据：\n",
      "    sepal_length  sepal_width  petal_length  petal_width species\n",
      "5           5.4          3.9           1.7          0.4  setosa\n",
      "6           4.6          3.4           1.4          0.3  setosa\n",
      "7           5.0          3.4           1.5          0.2  setosa\n",
      "8           4.4          2.9           1.4          0.2  setosa\n",
      "9           4.9          3.1           1.5          0.1  setosa\n",
      "填充缺失值后的数据：\n",
      "    sepal_length  sepal_width  petal_length  petal_width species\n",
      "0      5.877241          3.5           1.4          0.2  setosa\n",
      "1      5.877241          3.0           1.4          0.2  setosa\n",
      "2      5.877241          3.2           1.3          0.2  setosa\n",
      "3      5.877241          3.1           1.5          0.2  setosa\n",
      "4      5.877241          3.6           1.4          0.2  setosa\n"
     ]
    }
   ],
   "source": [
    "# 在 iris 数据集中人为添加缺失值\n",
    "iris_with_nan = iris.copy()\n",
    "iris_with_nan.iloc[0:5, 0] = None\n",
    "print(\"添加缺失值后的数据：\\n\", iris_with_nan.head())\n",
    "\n",
    "# 删除含有缺失值的行\n",
    "iris_dropped = iris_with_nan.dropna()\n",
    "print(\"删除缺失值后的数据：\\n\", iris_dropped.head())\n",
    "\n",
    "# 填充缺失值（使用列的平均值填充）\n",
    "iris_filled = iris_with_nan.fillna(iris_with_nan.mean())\n",
    "print(\"填充缺失值后的数据：\\n\", iris_filled.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fa569046",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sepal_length 大于 5 的数据：\n",
      "     sepal_length  sepal_width  petal_length  petal_width species\n",
      "0            5.1          3.5           1.4          0.2  setosa\n",
      "5            5.4          3.9           1.7          0.4  setosa\n",
      "10           5.4          3.7           1.5          0.2  setosa\n",
      "14           5.8          4.0           1.2          0.2  setosa\n",
      "15           5.7          4.4           1.5          0.4  setosa\n"
     ]
    }
   ],
   "source": [
    "# 筛选出 sepal_length 大于 5 的记录\n",
    "filtered_iris = iris[iris['sepal_length'] > 5]\n",
    "print(\"sepal_length 大于 5 的数据：\\n\", filtered_iris.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "50962c3a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "按 sepal_length 降序排序后的数据：\n",
      "      sepal_length  sepal_width  petal_length  petal_width    species\n",
      "131           7.9          3.8           6.4          2.0  virginica\n",
      "135           7.7          3.0           6.1          2.3  virginica\n",
      "122           7.7          2.8           6.7          2.0  virginica\n",
      "117           7.7          3.8           6.7          2.2  virginica\n",
      "118           7.7          2.6           6.9          2.3  virginica\n"
     ]
    }
   ],
   "source": [
    "# 按 sepal_length 进行排序\n",
    "sorted_iris = iris.sort_values(by='sepal_length', ascending=False)\n",
    "print(\"按 sepal_length 降序排序后的数据：\\n\", sorted_iris.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d5db487a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "是否有重复行： 1\n",
      "去重后的数据：\n",
      "    sepal_length  sepal_width  petal_length  petal_width species\n",
      "0           5.1          3.5           1.4          0.2  setosa\n",
      "1           4.9          3.0           1.4          0.2  setosa\n",
      "2           4.7          3.2           1.3          0.2  setosa\n",
      "3           4.6          3.1           1.5          0.2  setosa\n",
      "4           5.0          3.6           1.4          0.2  setosa\n"
     ]
    }
   ],
   "source": [
    "# 检查是否有重复行\n",
    "print(\"是否有重复行：\", iris.duplicated().sum())\n",
    "\n",
    "# 去重操作（Iris 数据集中没有重复行，但可以测试）\n",
    "iris_no_duplicates = iris.drop_duplicates()\n",
    "print(\"去重后的数据：\\n\", iris_no_duplicates.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "458f30b9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "透视表：\n",
      "             petal_length  sepal_length\n",
      "species                               \n",
      "setosa             1.462         5.006\n",
      "versicolor         4.260         5.936\n",
      "virginica          5.552         6.588\n"
     ]
    }
   ],
   "source": [
    "# 创建一个透视表，按种类（species）计算 sepal_length 和 petal_length 的均值\n",
    "pivot_table = pd.pivot_table(iris, values=['sepal_length', 'petal_length'], index='species', aggfunc='mean')\n",
    "print(\"透视表：\\n\", pivot_table)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7de90616",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "重命名列后的数据：\n",
      "    slength  swidth  petal_length  petal_width species\n",
      "0      5.1     3.5           1.4          0.2  setosa\n",
      "1      4.9     3.0           1.4          0.2  setosa\n",
      "2      4.7     3.2           1.3          0.2  setosa\n",
      "3      4.6     3.1           1.5          0.2  setosa\n",
      "4      5.0     3.6           1.4          0.2  setosa\n"
     ]
    }
   ],
   "source": [
    "# 重命名列名，将 sepal_length 改为 slength，sepal_width 改为 swidth\n",
    "renamed_iris = iris.rename(columns={'sepal_length': 'slength', 'sepal_width': 'swidth'})\n",
    "print(\"重命名列后的数据：\\n\", renamed_iris.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "3ddca744",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "添加新列后的数据：\n",
      "    sepal_length  sepal_width  petal_length  petal_width species  sepal_ratio\n",
      "0           5.1          3.5           1.4          0.2  setosa     1.457143\n",
      "1           4.9          3.0           1.4          0.2  setosa     1.633333\n",
      "2           4.7          3.2           1.3          0.2  setosa     1.468750\n",
      "3           4.6          3.1           1.5          0.2  setosa     1.483871\n",
      "4           5.0          3.6           1.4          0.2  setosa     1.388889\n"
     ]
    }
   ],
   "source": [
    "# 添加一个新列 'sepal_ratio'，等于 sepal_length / sepal_width\n",
    "iris['sepal_ratio'] = iris['sepal_length'] / iris['sepal_width']\n",
    "print(\"添加新列后的数据：\\n\", iris.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "16f0e098",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "删除列后的数据：\n",
      "    sepal_length  sepal_width  petal_length  petal_width species\n",
      "0           5.1          3.5           1.4          0.2  setosa\n",
      "1           4.9          3.0           1.4          0.2  setosa\n",
      "2           4.7          3.2           1.3          0.2  setosa\n",
      "3           4.6          3.1           1.5          0.2  setosa\n",
      "4           5.0          3.6           1.4          0.2  setosa\n"
     ]
    }
   ],
   "source": [
    "# 删除新添加的 'sepal_ratio' 列\n",
    "iris_dropped_col = iris.drop(columns=['sepal_ratio'])\n",
    "print(\"删除列后的数据：\\n\", iris_dropped_col.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "02bdb9fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "条件筛选后的数据：\n",
      "     sepal_length  sepal_width  petal_length  petal_width species  sepal_ratio\n",
      "5            5.4          3.9           1.7          0.4  setosa     1.384615\n",
      "11           4.8          3.4           1.6          0.2  setosa     1.411765\n",
      "18           5.7          3.8           1.7          0.3  setosa     1.500000\n",
      "20           5.4          3.4           1.7          0.2  setosa     1.588235\n",
      "23           5.1          3.3           1.7          0.5  setosa     1.545455\n"
     ]
    }
   ],
   "source": [
    "# 筛选出 petal_length 大于 1.5 且 species 为 'setosa' 的行\n",
    "filtered_data = iris[(iris['petal_length'] > 1.5) & (iris['species'] == 'setosa')]\n",
    "print(\"条件筛选后的数据：\\n\", filtered_data.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9fd79a6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "按索引选择的数据：\n",
      "    sepal_length species\n",
      "0           5.1  setosa\n",
      "1           4.9  setosa\n",
      "2           4.7  setosa\n",
      "3           4.6  setosa\n",
      "4           5.0  setosa\n",
      "5           5.4  setosa\n",
      "6           4.6  setosa\n",
      "7           5.0  setosa\n",
      "8           4.4  setosa\n",
      "9           4.9  setosa\n"
     ]
    }
   ],
   "source": [
    "# 按行索引选择前10行，按列索引选择 'sepal_length' 和 'species'\n",
    "selected_data = iris.loc[0:9, ['sepal_length', 'species']]\n",
    "print(\"按索引选择的数据：\\n\", selected_data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f8feea54",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "按位置选择的数据：\n",
      "    sepal_length  sepal_width\n",
      "0           5.1          3.5\n",
      "1           4.9          3.0\n",
      "2           4.7          3.2\n",
      "3           4.6          3.1\n",
      "4           5.0          3.6\n"
     ]
    }
   ],
   "source": [
    "# 按位置选择前5行和前两列\n",
    "selected_by_position = iris.iloc[0:5, 0:2]\n",
    "print(\"按位置选择的数据：\\n\", selected_by_position)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0e7aa3e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "应用自定义函数后的数据：\n",
      "    sepal_length  sepal_width  petal_length  petal_width species  sepal_ratio  \\\n",
      "0           5.1          3.5           1.4          0.2  setosa     1.457143   \n",
      "1           4.9          3.0           1.4          0.2  setosa     1.633333   \n",
      "2           4.7          3.2           1.3          0.2  setosa     1.468750   \n",
      "3           4.6          3.1           1.5          0.2  setosa     1.483871   \n",
      "4           5.0          3.6           1.4          0.2  setosa     1.388889   \n",
      "\n",
      "   sepal_product  \n",
      "0          17.85  \n",
      "1          14.70  \n",
      "2          15.04  \n",
      "3          14.26  \n",
      "4          18.00  \n"
     ]
    }
   ],
   "source": [
    "# 定义一个自定义函数，计算 sepal_length 和 sepal_width 的乘积\n",
    "def sepal_product(row):\n",
    "    return row['sepal_length'] * row['sepal_width']\n",
    "\n",
    "# 应用自定义函数，创建一个新列\n",
    "iris['sepal_product'] = iris.apply(sepal_product, axis=1)\n",
    "print(\"应用自定义函数后的数据：\\n\", iris.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d1258320",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "长表格式的数据：\n",
      "   species      variable  value\n",
      "0  setosa  sepal_length    5.1\n",
      "1  setosa  sepal_length    4.9\n",
      "2  setosa  sepal_length    4.7\n",
      "3  setosa  sepal_length    4.6\n",
      "4  setosa  sepal_length    5.0\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "Index contains duplicate entries, cannot reshape",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32mC:\\Windows\\Temp\\ipykernel_10964\\1373978531.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;31m# 使用 pivot 将长表转换回宽表\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mpivoted_iris\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmelted_iris\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpivot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'species'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'variable'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalues\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'value'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"宽表格式的数据：\\n\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpivoted_iris\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhead\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\AppData\\Roaming\\Python\\Python37\\site-packages\\pandas\\core\\frame.py\u001b[0m in \u001b[0;36mpivot\u001b[1;34m(self, index, columns, values)\u001b[0m\n\u001b[0;32m   6877\u001b[0m         \u001b[1;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcore\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpivot\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mpivot\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6878\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 6879\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mpivot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalues\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   6880\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6881\u001b[0m     _shared_docs[\n",
      "\u001b[1;32m~\\AppData\\Roaming\\Python\\Python37\\site-packages\\pandas\\core\\reshape\\pivot.py\u001b[0m in \u001b[0;36mpivot\u001b[1;34m(data, index, columns, values)\u001b[0m\n\u001b[0;32m    459\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    460\u001b[0m             \u001b[0mindexed\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_constructor_sliced\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_values\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 461\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0mindexed\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0munstack\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    462\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    463\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\AppData\\Roaming\\Python\\Python37\\site-packages\\pandas\\core\\series.py\u001b[0m in \u001b[0;36munstack\u001b[1;34m(self, level, fill_value)\u001b[0m\n\u001b[0;32m   3827\u001b[0m         \u001b[1;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcore\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreshape\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0munstack\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3828\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 3829\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0munstack\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfill_value\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   3830\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3831\u001b[0m     \u001b[1;31m# ----------------------------------------------------------------------\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\AppData\\Roaming\\Python\\Python37\\site-packages\\pandas\\core\\reshape\\reshape.py\u001b[0m in \u001b[0;36munstack\u001b[1;34m(obj, level, fill_value)\u001b[0m\n\u001b[0;32m    429\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0m_unstack_extension_series\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfill_value\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    430\u001b[0m         unstacker = _Unstacker(\n\u001b[1;32m--> 431\u001b[1;33m             \u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mlevel\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconstructor\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_constructor_expanddim\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    432\u001b[0m         )\n\u001b[0;32m    433\u001b[0m         return unstacker.get_result(\n",
      "\u001b[1;32m~\\AppData\\Roaming\\Python\\Python37\\site-packages\\pandas\\core\\reshape\\reshape.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, index, level, constructor)\u001b[0m\n\u001b[0;32m    116\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Unstacked DataFrame is too big, causing int32 overflow\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    117\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 118\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_make_selectors\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    119\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    120\u001b[0m     \u001b[1;33m@\u001b[0m\u001b[0mcache_readonly\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\AppData\\Roaming\\Python\\Python37\\site-packages\\pandas\\core\\reshape\\reshape.py\u001b[0m in \u001b[0;36m_make_selectors\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    165\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    166\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mmask\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m<\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 167\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Index contains duplicate entries, cannot reshape\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    168\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    169\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgroup_index\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcomp_index\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: Index contains duplicate entries, cannot reshape"
     ]
    }
   ],
   "source": [
    "# 使用 melt 将宽表转换为长表\n",
    "melted_iris = pd.melt(iris, id_vars='species', value_vars=['sepal_length', 'sepal_width'])\n",
    "print(\"长表格式的数据：\\n\", melted_iris.head())\n",
    "\n",
    "# 使用 pivot 将长表转换回宽表\n",
    "pivoted_iris = melted_iris.pivot(index='species', columns='variable', values='value')\n",
    "print(\"宽表格式的数据：\\n\", pivoted_iris.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c63d2ce1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "累积求和后的数据：\n",
      "    sepal_length  sepal_width  petal_length  petal_width species  sepal_ratio  \\\n",
      "0           5.1          3.5           1.4          0.2  setosa     1.457143   \n",
      "1           4.9          3.0           1.4          0.2  setosa     1.633333   \n",
      "2           4.7          3.2           1.3          0.2  setosa     1.468750   \n",
      "3           4.6          3.1           1.5          0.2  setosa     1.483871   \n",
      "4           5.0          3.6           1.4          0.2  setosa     1.388889   \n",
      "\n",
      "   sepal_product  sepal_cumsum  \n",
      "0          17.85           5.1  \n",
      "1          14.70          10.0  \n",
      "2          15.04          14.7  \n",
      "3          14.26          19.3  \n",
      "4          18.00          24.3  \n"
     ]
    }
   ],
   "source": [
    "# 对 'sepal_length' 列执行累积求和\n",
    "iris['sepal_cumsum'] = iris['sepal_length'].cumsum()\n",
    "print(\"累积求和后的数据：\\n\", iris.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c9930053",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机抽取的数据：\n",
      "     sepal_length  sepal_width  petal_length  petal_width     species  \\\n",
      "18           5.7          3.8           1.7          0.3      setosa   \n",
      "5            5.4          3.9           1.7          0.4      setosa   \n",
      "93           5.0          2.3           3.3          1.0  versicolor   \n",
      "3            4.6          3.1           1.5          0.2      setosa   \n",
      "85           6.0          3.4           4.5          1.6  versicolor   \n",
      "\n",
      "    sepal_ratio  sepal_product  sepal_cumsum  \n",
      "18     1.500000          21.66          95.6  \n",
      "5      1.384615          21.06          29.7  \n",
      "93     2.173913          11.50         513.1  \n",
      "3      1.483871          14.26          19.3  \n",
      "85     1.764706          20.40         466.6  \n"
     ]
    }
   ],
   "source": [
    "# 从数据集中随机抽取5条记录\n",
    "sample_data = iris.sample(n=5)\n",
    "print(\"随机抽取的数据：\\n\", sample_data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "942a4bb9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "连接后的数据：\n",
      "      sepal_length  sepal_width  petal_length  petal_width    species  \\\n",
      "0             5.1          3.5           1.4          0.2     setosa   \n",
      "1             4.9          3.0           1.4          0.2     setosa   \n",
      "2             4.7          3.2           1.3          0.2     setosa   \n",
      "3             4.6          3.1           1.5          0.2     setosa   \n",
      "4             5.0          3.6           1.4          0.2     setosa   \n",
      "145           6.7          3.0           5.2          2.3  virginica   \n",
      "146           6.3          2.5           5.0          1.9  virginica   \n",
      "147           6.5          3.0           5.2          2.0  virginica   \n",
      "148           6.2          3.4           5.4          2.3  virginica   \n",
      "149           5.9          3.0           5.1          1.8  virginica   \n",
      "\n",
      "     sepal_ratio  sepal_product  sepal_cumsum  \n",
      "0       1.457143          17.85           5.1  \n",
      "1       1.633333          14.70          10.0  \n",
      "2       1.468750          15.04          14.7  \n",
      "3       1.483871          14.26          19.3  \n",
      "4       1.388889          18.00          24.3  \n",
      "145     2.233333          20.10         851.6  \n",
      "146     2.520000          15.75         857.9  \n",
      "147     2.166667          19.50         864.4  \n",
      "148     1.823529          21.08         870.6  \n",
      "149     1.966667          17.70         876.5  \n"
     ]
    }
   ],
   "source": [
    "# 将数据框沿行轴（axis=0）连接\n",
    "iris_concat = pd.concat([iris.head(), iris.tail()], axis=0)\n",
    "print(\"连接后的数据：\\n\", iris_concat)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "505dd530",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "条件修改后的数据：\n",
      "    sepal_length  sepal_width  petal_length  petal_width species  sepal_ratio  \\\n",
      "0          10.2          3.5           1.4          0.2  setosa     1.457143   \n",
      "1           9.8          3.0           1.4          0.2  setosa     1.633333   \n",
      "2           9.4          3.2           1.3          0.2  setosa     1.468750   \n",
      "3           9.2          3.1           1.5          0.2  setosa     1.483871   \n",
      "4          10.0          3.6           1.4          0.2  setosa     1.388889   \n",
      "\n",
      "   sepal_product  sepal_cumsum  \n",
      "0          17.85           5.1  \n",
      "1          14.70          10.0  \n",
      "2          15.04          14.7  \n",
      "3          14.26          19.3  \n",
      "4          18.00          24.3  \n"
     ]
    }
   ],
   "source": [
    "# 将所有 species 为 'setosa' 的记录的 sepal_length 乘以 2\n",
    "iris.loc[iris['species'] == 'setosa', 'sepal_length'] *= 2\n",
    "print(\"条件修改后的数据：\\n\", iris.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f6a14721",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "带有日期列的数据：\n",
      "    sepal_length  sepal_width  petal_length  petal_width species  sepal_ratio  \\\n",
      "0          10.2          3.5           1.4          0.2  setosa     1.457143   \n",
      "1           9.8          3.0           1.4          0.2  setosa     1.633333   \n",
      "2           9.4          3.2           1.3          0.2  setosa     1.468750   \n",
      "3           9.2          3.1           1.5          0.2  setosa     1.483871   \n",
      "4          10.0          3.6           1.4          0.2  setosa     1.388889   \n",
      "\n",
      "   sepal_product  sepal_cumsum       date  \n",
      "0          17.85           5.1 2024-01-01  \n",
      "1          14.70          10.0 2024-01-02  \n",
      "2          15.04          14.7 2024-01-03  \n",
      "3          14.26          19.3 2024-01-04  \n",
      "4          18.00          24.3 2024-01-05  \n",
      "设置日期为索引后的数据：\n",
      "             sepal_length  sepal_width  petal_length  petal_width species  \\\n",
      "date                                                                       \n",
      "2024-01-01          10.2          3.5           1.4          0.2  setosa   \n",
      "2024-01-02           9.8          3.0           1.4          0.2  setosa   \n",
      "2024-01-03           9.4          3.2           1.3          0.2  setosa   \n",
      "2024-01-04           9.2          3.1           1.5          0.2  setosa   \n",
      "2024-01-05          10.0          3.6           1.4          0.2  setosa   \n",
      "\n",
      "            sepal_ratio  sepal_product  sepal_cumsum  \n",
      "date                                                  \n",
      "2024-01-01     1.457143          17.85           5.1  \n",
      "2024-01-02     1.633333          14.70          10.0  \n",
      "2024-01-03     1.468750          15.04          14.7  \n",
      "2024-01-04     1.483871          14.26          19.3  \n",
      "2024-01-05     1.388889          18.00          24.3  \n"
     ]
    }
   ],
   "source": [
    "# 创建一个新的日期列，表示从某个时间开始的日期\n",
    "import numpy as np\n",
    "iris['date'] = pd.date_range(start='2024-01-01', periods=len(iris), freq='D')\n",
    "print(\"带有日期列的数据：\\n\", iris.head())\n",
    "\n",
    "# 将日期列设置为索引\n",
    "iris.set_index('date', inplace=True)\n",
    "print(\"设置日期为索引后的数据：\\n\", iris.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "7b4664c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "透视表（多重聚合）：\n",
      "                    mean                       sum             \n",
      "           petal_length sepal_length petal_length sepal_length\n",
      "species                                                       \n",
      "setosa            1.462       10.012         73.1        500.6\n",
      "versicolor        4.260        5.936        213.0        296.8\n",
      "virginica         5.552        6.588        277.6        329.4\n"
     ]
    }
   ],
   "source": [
    "# 创建一个透视表，对 sepal_length 和 petal_length 进行求和和求均值操作\n",
    "pivot_table_multiagg = pd.pivot_table(iris, values=['sepal_length', 'petal_length'], index='species', aggfunc=[np.mean, np.sum])\n",
    "print(\"透视表（多重聚合）：\\n\", pivot_table_multiagg)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "1b1aa9ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "多级索引的数据：\n",
      "                 sepal_length  sepal_width  petal_length  petal_width  \\\n",
      "species source                                                         \n",
      "setosa  garden          10.2          3.5           1.4          0.2   \n",
      "        garden           9.8          3.0           1.4          0.2   \n",
      "        garden           9.4          3.2           1.3          0.2   \n",
      "        garden           9.2          3.1           1.5          0.2   \n",
      "        garden          10.0          3.6           1.4          0.2   \n",
      "\n",
      "                sepal_ratio  sepal_product  sepal_cumsum  \n",
      "species source                                            \n",
      "setosa  garden     1.457143          17.85           5.1  \n",
      "        garden     1.633333          14.70          10.0  \n",
      "        garden     1.468750          15.04          14.7  \n",
      "        garden     1.483871          14.26          19.3  \n",
      "        garden     1.388889          18.00          24.3  \n",
      "选择特定索引的数据：\n",
      "                 sepal_length  sepal_width  petal_length  petal_width  \\\n",
      "species source                                                         \n",
      "setosa  garden          10.2          3.5           1.4          0.2   \n",
      "        garden           9.8          3.0           1.4          0.2   \n",
      "        garden           9.4          3.2           1.3          0.2   \n",
      "        garden           9.2          3.1           1.5          0.2   \n",
      "        garden          10.0          3.6           1.4          0.2   \n",
      "        garden          10.8          3.9           1.7          0.4   \n",
      "        garden           9.2          3.4           1.4          0.3   \n",
      "        garden          10.0          3.4           1.5          0.2   \n",
      "        garden           8.8          2.9           1.4          0.2   \n",
      "        garden           9.8          3.1           1.5          0.1   \n",
      "        garden          10.8          3.7           1.5          0.2   \n",
      "        garden           9.6          3.4           1.6          0.2   \n",
      "        garden           9.6          3.0           1.4          0.1   \n",
      "        garden           8.6          3.0           1.1          0.1   \n",
      "        garden          11.6          4.0           1.2          0.2   \n",
      "        garden          11.4          4.4           1.5          0.4   \n",
      "        garden          10.8          3.9           1.3          0.4   \n",
      "        garden          10.2          3.5           1.4          0.3   \n",
      "        garden          11.4          3.8           1.7          0.3   \n",
      "        garden          10.2          3.8           1.5          0.3   \n",
      "        garden          10.8          3.4           1.7          0.2   \n",
      "        garden          10.2          3.7           1.5          0.4   \n",
      "        garden           9.2          3.6           1.0          0.2   \n",
      "        garden          10.2          3.3           1.7          0.5   \n",
      "        garden           9.6          3.4           1.9          0.2   \n",
      "        garden          10.0          3.0           1.6          0.2   \n",
      "        garden          10.0          3.4           1.6          0.4   \n",
      "        garden          10.4          3.5           1.5          0.2   \n",
      "        garden          10.4          3.4           1.4          0.2   \n",
      "        garden           9.4          3.2           1.6          0.2   \n",
      "        garden           9.6          3.1           1.6          0.2   \n",
      "        garden          10.8          3.4           1.5          0.4   \n",
      "        garden          10.4          4.1           1.5          0.1   \n",
      "        garden          11.0          4.2           1.4          0.2   \n",
      "        garden           9.8          3.1           1.5          0.2   \n",
      "        garden          10.0          3.2           1.2          0.2   \n",
      "        garden          11.0          3.5           1.3          0.2   \n",
      "        garden           9.8          3.6           1.4          0.1   \n",
      "        garden           8.8          3.0           1.3          0.2   \n",
      "        garden          10.2          3.4           1.5          0.2   \n",
      "        garden          10.0          3.5           1.3          0.3   \n",
      "        garden           9.0          2.3           1.3          0.3   \n",
      "        garden           8.8          3.2           1.3          0.2   \n",
      "        garden          10.0          3.5           1.6          0.6   \n",
      "        garden          10.2          3.8           1.9          0.4   \n",
      "        garden           9.6          3.0           1.4          0.3   \n",
      "        garden          10.2          3.8           1.6          0.2   \n",
      "        garden           9.2          3.2           1.4          0.2   \n",
      "        garden          10.6          3.7           1.5          0.2   \n",
      "        garden          10.0          3.3           1.4          0.2   \n",
      "\n",
      "                sepal_ratio  sepal_product  sepal_cumsum  \n",
      "species source                                            \n",
      "setosa  garden     1.457143          17.85           5.1  \n",
      "        garden     1.633333          14.70          10.0  \n",
      "        garden     1.468750          15.04          14.7  \n",
      "        garden     1.483871          14.26          19.3  \n",
      "        garden     1.388889          18.00          24.3  \n",
      "        garden     1.384615          21.06          29.7  \n",
      "        garden     1.352941          15.64          34.3  \n",
      "        garden     1.470588          17.00          39.3  \n",
      "        garden     1.517241          12.76          43.7  \n",
      "        garden     1.580645          15.19          48.6  \n",
      "        garden     1.459459          19.98          54.0  \n",
      "        garden     1.411765          16.32          58.8  \n",
      "        garden     1.600000          14.40          63.6  \n",
      "        garden     1.433333          12.90          67.9  \n",
      "        garden     1.450000          23.20          73.7  \n",
      "        garden     1.295455          25.08          79.4  \n",
      "        garden     1.384615          21.06          84.8  \n",
      "        garden     1.457143          17.85          89.9  \n",
      "        garden     1.500000          21.66          95.6  \n",
      "        garden     1.342105          19.38         100.7  \n",
      "        garden     1.588235          18.36         106.1  \n",
      "        garden     1.378378          18.87         111.2  \n",
      "        garden     1.277778          16.56         115.8  \n",
      "        garden     1.545455          16.83         120.9  \n",
      "        garden     1.411765          16.32         125.7  \n",
      "        garden     1.666667          15.00         130.7  \n",
      "        garden     1.470588          17.00         135.7  \n",
      "        garden     1.485714          18.20         140.9  \n",
      "        garden     1.529412          17.68         146.1  \n",
      "        garden     1.468750          15.04         150.8  \n",
      "        garden     1.548387          14.88         155.6  \n",
      "        garden     1.588235          18.36         161.0  \n",
      "        garden     1.268293          21.32         166.2  \n",
      "        garden     1.309524          23.10         171.7  \n",
      "        garden     1.580645          15.19         176.6  \n",
      "        garden     1.562500          16.00         181.6  \n",
      "        garden     1.571429          19.25         187.1  \n",
      "        garden     1.361111          17.64         192.0  \n",
      "        garden     1.466667          13.20         196.4  \n",
      "        garden     1.500000          17.34         201.5  \n",
      "        garden     1.428571          17.50         206.5  \n",
      "        garden     1.956522          10.35         211.0  \n",
      "        garden     1.375000          14.08         215.4  \n",
      "        garden     1.428571          17.50         220.4  \n",
      "        garden     1.342105          19.38         225.5  \n",
      "        garden     1.600000          14.40         230.3  \n",
      "        garden     1.342105          19.38         235.4  \n",
      "        garden     1.437500          14.72         240.0  \n",
      "        garden     1.432432          19.61         245.3  \n",
      "        garden     1.515152          16.50         250.3  \n"
     ]
    }
   ],
   "source": [
    "# 创建一个新的列 'source'，并与 species 一起构成多级索引\n",
    "iris['source'] = ['garden'] * 50 + ['wild'] * 50 + ['botanic'] * 50\n",
    "iris_multiindex = iris.set_index(['species', 'source'])\n",
    "print(\"多级索引的数据：\\n\", iris_multiindex.head())\n",
    "\n",
    "# 从多级索引中选择某一类\n",
    "print(\"选择特定索引的数据：\\n\", iris_multiindex.loc[('setosa', 'garden')])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "749bfb02",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "滚动平均值后的数据：\n",
      "             sepal_length  sepal_width  petal_length  petal_width species  \\\n",
      "date                                                                       \n",
      "2024-01-01          10.2          3.5           1.4          0.2  setosa   \n",
      "2024-01-02           9.8          3.0           1.4          0.2  setosa   \n",
      "2024-01-03           9.4          3.2           1.3          0.2  setosa   \n",
      "2024-01-04           9.2          3.1           1.5          0.2  setosa   \n",
      "2024-01-05          10.0          3.6           1.4          0.2  setosa   \n",
      "\n",
      "            sepal_ratio  sepal_product  sepal_cumsum  source  \\\n",
      "date                                                           \n",
      "2024-01-01     1.457143          17.85           5.1  garden   \n",
      "2024-01-02     1.633333          14.70          10.0  garden   \n",
      "2024-01-03     1.468750          15.04          14.7  garden   \n",
      "2024-01-04     1.483871          14.26          19.3  garden   \n",
      "2024-01-05     1.388889          18.00          24.3  garden   \n",
      "\n",
      "            sepal_rolling_mean  \n",
      "date                            \n",
      "2024-01-01                 NaN  \n",
      "2024-01-02                 NaN  \n",
      "2024-01-03            9.800000  \n",
      "2024-01-04            9.466667  \n",
      "2024-01-05            9.533333  \n"
     ]
    }
   ],
   "source": [
    "# 计算 sepal_length 的滚动平均值，窗口大小为 3\n",
    "iris['sepal_rolling_mean'] = iris['sepal_length'].rolling(window=3).mean()\n",
    "print(\"滚动平均值后的数据：\\n\", iris.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "b7010b24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "查询语句筛选的数据：\n",
      "             sepal_length  sepal_width  petal_length  petal_width species  \\\n",
      "date                                                                       \n",
      "2024-01-01          10.2          3.5           1.4          0.2  setosa   \n",
      "2024-01-02           9.8          3.0           1.4          0.2  setosa   \n",
      "2024-01-03           9.4          3.2           1.3          0.2  setosa   \n",
      "2024-01-04           9.2          3.1           1.5          0.2  setosa   \n",
      "2024-01-05          10.0          3.6           1.4          0.2  setosa   \n",
      "2024-01-06          10.8          3.9           1.7          0.4  setosa   \n",
      "2024-01-07           9.2          3.4           1.4          0.3  setosa   \n",
      "2024-01-08          10.0          3.4           1.5          0.2  setosa   \n",
      "2024-01-09           8.8          2.9           1.4          0.2  setosa   \n",
      "2024-01-10           9.8          3.1           1.5          0.1  setosa   \n",
      "2024-01-11          10.8          3.7           1.5          0.2  setosa   \n",
      "2024-01-12           9.6          3.4           1.6          0.2  setosa   \n",
      "2024-01-13           9.6          3.0           1.4          0.1  setosa   \n",
      "2024-01-14           8.6          3.0           1.1          0.1  setosa   \n",
      "2024-01-15          11.6          4.0           1.2          0.2  setosa   \n",
      "2024-01-16          11.4          4.4           1.5          0.4  setosa   \n",
      "2024-01-17          10.8          3.9           1.3          0.4  setosa   \n",
      "2024-01-18          10.2          3.5           1.4          0.3  setosa   \n",
      "2024-01-19          11.4          3.8           1.7          0.3  setosa   \n",
      "2024-01-20          10.2          3.8           1.5          0.3  setosa   \n",
      "2024-01-21          10.8          3.4           1.7          0.2  setosa   \n",
      "2024-01-22          10.2          3.7           1.5          0.4  setosa   \n",
      "2024-01-23           9.2          3.6           1.0          0.2  setosa   \n",
      "2024-01-24          10.2          3.3           1.7          0.5  setosa   \n",
      "2024-01-25           9.6          3.4           1.9          0.2  setosa   \n",
      "2024-01-26          10.0          3.0           1.6          0.2  setosa   \n",
      "2024-01-27          10.0          3.4           1.6          0.4  setosa   \n",
      "2024-01-28          10.4          3.5           1.5          0.2  setosa   \n",
      "2024-01-29          10.4          3.4           1.4          0.2  setosa   \n",
      "2024-01-30           9.4          3.2           1.6          0.2  setosa   \n",
      "2024-01-31           9.6          3.1           1.6          0.2  setosa   \n",
      "2024-02-01          10.8          3.4           1.5          0.4  setosa   \n",
      "2024-02-02          10.4          4.1           1.5          0.1  setosa   \n",
      "2024-02-03          11.0          4.2           1.4          0.2  setosa   \n",
      "2024-02-04           9.8          3.1           1.5          0.2  setosa   \n",
      "2024-02-05          10.0          3.2           1.2          0.2  setosa   \n",
      "2024-02-06          11.0          3.5           1.3          0.2  setosa   \n",
      "2024-02-07           9.8          3.6           1.4          0.1  setosa   \n",
      "2024-02-08           8.8          3.0           1.3          0.2  setosa   \n",
      "2024-02-09          10.2          3.4           1.5          0.2  setosa   \n",
      "2024-02-10          10.0          3.5           1.3          0.3  setosa   \n",
      "2024-02-11           9.0          2.3           1.3          0.3  setosa   \n",
      "2024-02-12           8.8          3.2           1.3          0.2  setosa   \n",
      "2024-02-13          10.0          3.5           1.6          0.6  setosa   \n",
      "2024-02-14          10.2          3.8           1.9          0.4  setosa   \n",
      "2024-02-15           9.6          3.0           1.4          0.3  setosa   \n",
      "2024-02-16          10.2          3.8           1.6          0.2  setosa   \n",
      "2024-02-17           9.2          3.2           1.4          0.2  setosa   \n",
      "2024-02-18          10.6          3.7           1.5          0.2  setosa   \n",
      "2024-02-19          10.0          3.3           1.4          0.2  setosa   \n",
      "\n",
      "            sepal_ratio  sepal_product  sepal_cumsum  source  \\\n",
      "date                                                           \n",
      "2024-01-01     1.457143          17.85           5.1  garden   \n",
      "2024-01-02     1.633333          14.70          10.0  garden   \n",
      "2024-01-03     1.468750          15.04          14.7  garden   \n",
      "2024-01-04     1.483871          14.26          19.3  garden   \n",
      "2024-01-05     1.388889          18.00          24.3  garden   \n",
      "2024-01-06     1.384615          21.06          29.7  garden   \n",
      "2024-01-07     1.352941          15.64          34.3  garden   \n",
      "2024-01-08     1.470588          17.00          39.3  garden   \n",
      "2024-01-09     1.517241          12.76          43.7  garden   \n",
      "2024-01-10     1.580645          15.19          48.6  garden   \n",
      "2024-01-11     1.459459          19.98          54.0  garden   \n",
      "2024-01-12     1.411765          16.32          58.8  garden   \n",
      "2024-01-13     1.600000          14.40          63.6  garden   \n",
      "2024-01-14     1.433333          12.90          67.9  garden   \n",
      "2024-01-15     1.450000          23.20          73.7  garden   \n",
      "2024-01-16     1.295455          25.08          79.4  garden   \n",
      "2024-01-17     1.384615          21.06          84.8  garden   \n",
      "2024-01-18     1.457143          17.85          89.9  garden   \n",
      "2024-01-19     1.500000          21.66          95.6  garden   \n",
      "2024-01-20     1.342105          19.38         100.7  garden   \n",
      "2024-01-21     1.588235          18.36         106.1  garden   \n",
      "2024-01-22     1.378378          18.87         111.2  garden   \n",
      "2024-01-23     1.277778          16.56         115.8  garden   \n",
      "2024-01-24     1.545455          16.83         120.9  garden   \n",
      "2024-01-25     1.411765          16.32         125.7  garden   \n",
      "2024-01-26     1.666667          15.00         130.7  garden   \n",
      "2024-01-27     1.470588          17.00         135.7  garden   \n",
      "2024-01-28     1.485714          18.20         140.9  garden   \n",
      "2024-01-29     1.529412          17.68         146.1  garden   \n",
      "2024-01-30     1.468750          15.04         150.8  garden   \n",
      "2024-01-31     1.548387          14.88         155.6  garden   \n",
      "2024-02-01     1.588235          18.36         161.0  garden   \n",
      "2024-02-02     1.268293          21.32         166.2  garden   \n",
      "2024-02-03     1.309524          23.10         171.7  garden   \n",
      "2024-02-04     1.580645          15.19         176.6  garden   \n",
      "2024-02-05     1.562500          16.00         181.6  garden   \n",
      "2024-02-06     1.571429          19.25         187.1  garden   \n",
      "2024-02-07     1.361111          17.64         192.0  garden   \n",
      "2024-02-08     1.466667          13.20         196.4  garden   \n",
      "2024-02-09     1.500000          17.34         201.5  garden   \n",
      "2024-02-10     1.428571          17.50         206.5  garden   \n",
      "2024-02-11     1.956522          10.35         211.0  garden   \n",
      "2024-02-12     1.375000          14.08         215.4  garden   \n",
      "2024-02-13     1.428571          17.50         220.4  garden   \n",
      "2024-02-14     1.342105          19.38         225.5  garden   \n",
      "2024-02-15     1.600000          14.40         230.3  garden   \n",
      "2024-02-16     1.342105          19.38         235.4  garden   \n",
      "2024-02-17     1.437500          14.72         240.0  garden   \n",
      "2024-02-18     1.432432          19.61         245.3  garden   \n",
      "2024-02-19     1.515152          16.50         250.3  garden   \n",
      "\n",
      "            sepal_rolling_mean  \n",
      "date                            \n",
      "2024-01-01                 NaN  \n",
      "2024-01-02                 NaN  \n",
      "2024-01-03            9.800000  \n",
      "2024-01-04            9.466667  \n",
      "2024-01-05            9.533333  \n",
      "2024-01-06           10.000000  \n",
      "2024-01-07           10.000000  \n",
      "2024-01-08           10.000000  \n",
      "2024-01-09            9.333333  \n",
      "2024-01-10            9.533333  \n",
      "2024-01-11            9.800000  \n",
      "2024-01-12           10.066667  \n",
      "2024-01-13           10.000000  \n",
      "2024-01-14            9.266667  \n",
      "2024-01-15            9.933333  \n",
      "2024-01-16           10.533333  \n",
      "2024-01-17           11.266667  \n",
      "2024-01-18           10.800000  \n",
      "2024-01-19           10.800000  \n",
      "2024-01-20           10.600000  \n",
      "2024-01-21           10.800000  \n",
      "2024-01-22           10.400000  \n",
      "2024-01-23           10.066667  \n",
      "2024-01-24            9.866667  \n",
      "2024-01-25            9.666667  \n",
      "2024-01-26            9.933333  \n",
      "2024-01-27            9.866667  \n",
      "2024-01-28           10.133333  \n",
      "2024-01-29           10.266667  \n",
      "2024-01-30           10.066667  \n",
      "2024-01-31            9.800000  \n",
      "2024-02-01            9.933333  \n",
      "2024-02-02           10.266667  \n",
      "2024-02-03           10.733333  \n",
      "2024-02-04           10.400000  \n",
      "2024-02-05           10.266667  \n",
      "2024-02-06           10.266667  \n",
      "2024-02-07           10.266667  \n",
      "2024-02-08            9.866667  \n",
      "2024-02-09            9.600000  \n",
      "2024-02-10            9.666667  \n",
      "2024-02-11            9.733333  \n",
      "2024-02-12            9.266667  \n",
      "2024-02-13            9.266667  \n",
      "2024-02-14            9.666667  \n",
      "2024-02-15            9.933333  \n",
      "2024-02-16           10.000000  \n",
      "2024-02-17            9.666667  \n",
      "2024-02-18           10.000000  \n",
      "2024-02-19            9.933333  \n"
     ]
    }
   ],
   "source": [
    "# 使用 query 方法选择 sepal_length 大于 5 且 species 为 'setosa' 的行\n",
    "query_data = iris.query('sepal_length > 5 and species == \"setosa\"')\n",
    "print(\"查询语句筛选的数据：\\n\", query_data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "8ec11549",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "按月重采样的数据：\n",
      "             sepal_length  sepal_width  petal_length  petal_width  sepal_ratio  \\\n",
      "date                                                                            \n",
      "2024-01-31     10.038710     3.438710      1.477419     0.245161     1.465904   \n",
      "2024-02-29      8.634483     3.224138      2.448276     0.637931     1.702781   \n",
      "2024-03-31      5.964516     2.745161      4.303226     1.335484     2.197244   \n",
      "2024-04-30      6.300000     2.876667      5.156667     1.810000     2.203419   \n",
      "2024-05-31      6.596552     3.003448      5.475862     2.003448     2.207287   \n",
      "\n",
      "            sepal_product  sepal_cumsum  sepal_rolling_mean  \n",
      "date                                                         \n",
      "2024-01-31      17.357097     79.900000           10.055172  \n",
      "2024-02-29      17.297586    233.251724            8.767816  \n",
      "2024-03-31      16.425161    406.806452            5.969892  \n",
      "2024-04-30      18.274667    590.503333            6.263333  \n",
      "2024-05-31      19.872759    785.062069            6.617241  \n"
     ]
    }
   ],
   "source": [
    "# 按月重采样，计算每月的平均值\n",
    "monthly_resample = iris.resample('M').mean()\n",
    "print(\"按月重采样的数据：\\n\", monthly_resample)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3efaf8be",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mathjian",
   "language": "python",
   "name": "mathjian"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
